<?php
namespace app\admin\controller;
use think\Db;
use think\Session;
use think\Request;

class Role extends Admincontroller
{
	/**
	 * 加载身份列表页面
	 * @return [type] [description]
	 */
	public function index()
	{
		$name = Db::name('admin')->field('name')->where('name = "'.Session::get('admin').'"')->find();

		// 查询身份表数据
		$list = Db::name('role')->select();

		return $this->fetch('list',['title'=>'蒸汽男孩后台','nick'=>$name['name'],'list'=>$list]);
	}

	/**
	 * 加载增加身份页面
	 */
	public function add()
	{
		$name = Db::name('admin')->field('name')->where('name = "'.Session::get('admin').'"')->find();

		return $this->fetch('add',['title'=>'蒸汽男孩后台','nick'=>$name['name']]);
	}

	/**
	 * 执行增加身份逻辑
	 * @return [type] [description]
	 */
	public function insert()
	{
		$name = $_POST['name'];
		$remark = $_POST['remark'];
		$status = $_POST['status'];
		$data = Db::name('role')->insert(['name'=>$name,'remark'=>$remark,'status'=>$status]);

		if($data > 0) {
			$this->success('添加成功',url('admin/role/index'));
		} else {
			$this->error('添加失败,请重试');
		}

	}

	/**
	 * 执行删除身份逻辑
	 * @param  [type] $id [description]
	 * @return [type]     [description]
	 */
	public function delete($id)
	{
		$result = Db::name('role')->delete($id);
		if( $result>0 ) {
			$this->success('删除成功',url('admin/role/index'));
		}else{
			$this->error('删除失败');
		}
	}

	/**
	 * 加载编辑身份页面
	 * @param  [type] $id [description]
	 * @return [type]     [description]
	 */
	public function edit($id)
	{
		$name = Db::name('admin')->field('name')->where('name = "'.Session::get('admin').'"')->find();

		$data = Db::name('role')->find($id);
		return $this->fetch('edit',['title'=>'蒸汽男孩后台','nick'=>$name['name'],'data'=>$data]);
	}

	/**
	 * 执行编辑身份逻辑
	 * @param  [type] $id [description]
	 * @return [type]     [description]
	 */
	public function update($id)
	{

		$name = $_POST['name'];
		$remark = $_POST['remark'];
		$status = $_POST['status'];
		$result = Db::name('role')->where('id = "'.$id.'"')->update(['name'=>$name,'remark'=>$remark,'status'=>$status]);
		if($result > 0) {
			$this->success('修改成功',url('admin/role/index'));
		} else {
			$this->error('修改失败,请重试');
		}
	}

	/**
	 * 加载分配节点页面
	 * @param  [type] $id [description]
	 * @return [type]     [description]
	 */
	public function qx($id)
	{
		$name = Db::name('admin')->field('name')->where('name = "'.Session::get('admin').'"')->find();

		// 查询身份所有的节点

		$have_node = Db::table('ubisoft_role r,ubisoft_node n,ubisoft_role_node q')->field('n.name')->where('r.id = q.rid and q.nid = n.id and r.id = "'.$id.'"')->select();
		$haves = array();
		// 遍历节点
		foreach ($have_node as $k => $v) {
			$haves[] = $v['name'];
		}

		$node = Db::name('node')->select();

		return $this->fetch('qx_edit',['title'=>'蒸汽男孩后台','nick'=>$name['name'],'node'=>$node,'haves'=>$haves,'id'=>$id]);
	}

	/**
	 * 执行分配节点逻辑
	 * @param  Request $req [description]
	 * @return [type]       [description]
	 */
	public function update_qx(Request $req)
	{
		$sql = $req->post();

		if(empty($sql)){
			$this->error("必须选择一个节点！");
		}
		// 通过模板获得所有节点的id 遍历数组
		$rids = array();
		foreach ($sql['nid'] as $k => $v) {
			$nids[] = $v;
		}
		// 存入一个新数组
		$rids = $sql['rid'];

		// 执行时先将对应身份的节点全部删除
		Db::name('role_node')->where(['rid' => $rids])->delete();

		// 在循环加入表单打勾的节点
		foreach ($nids as $k => $v) {
			$role_node['nid'] = $v;
            $role_node['rid'] = $rids;
            $data = db('role_node')->data($role_node)->insert();
		}

		if($data > 0) {
			$this->success('分配成功',url('admin/role/index'));
		} else {
			$this->error('分配失败');
		}
	}
}